<?php

/**
 * @version		$Id: vote.php 21097 2011-04-07 15:38:03Z dextercowley $
 * @copyright	Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */
// No direct access.
defined('_JEXEC') or die;

jimport('joomla.plugin.plugin');

/**
 * Xprop plugin.
 *
 * @package		Joomla.Plugin
 * @subpackage	Content.vote
 */
class plgContentXprop extends JPlugin {

	/**
	 * Constructor
	 *
	 * @access      protected
	 * @param       object  $subject The object to observe
	 * @param       array   $config  An array that holds the plugin configuration
	 * @since       1.5
	 */
	public function __construct(& $subject, $config) {
		parent::__construct($subject, $config);
		$this->loadLanguage();
	}

	/**
	 * @since	1.6
	 */
	public function onContentBeforeDisplay($context, &$row, &$params, $page = 0) {
		
	}

	public function onContentAfterDisplay($context, &$row, &$params, $page = 0) {
		
	}

	public function onContentPrepareForm(JForm $form, $data) {
		// Run only in administration context
		if (!strpos(JPATH_BASE,'administrator')) return;
		// As for joomla < 2.5, add some thumbs to any content
		$xml = JFactory::getXML(dirname(JPATH_BASE) . DS . 'plugins' . DS . 'content' . DS . 'xprop' . DS . 'article' . DS . "thumbnail.xml");
		$fields = $xml->xpath('//fields');

		$form->setFields($fields);

		$catid = '';
		
		if (empty($data))
			$data = JRequest::getVar('jform', array(), 'post', 'array');

		if (is_object($data) && isset($data->catid))
			$catid = $data->catid;
		else if (is_array($data) && isset($data['catid']))
			$catid = $data['catid'];
		// Decide xprop data-type base on plugin-config
		// and add extra properties to the editor form by merging an external property file. 
		// Hence xprop the name.
		$type = null;
		
		if (self::getArticleType ($catid, $type)){
			$xml = JFactory::getXML(dirname(JPATH_BASE) . DS . 'plugins' . DS . 'content' . DS . 'xprop' . DS . 'article' . DS . "$type.xml");
			$fields = $xml->xpath('//fields');

			$form->setFields($fields);
		}
	}

	public function onContentBeforeSave($context, &$table, $isNew) {
		
	}

	public function onContentPrepare($context, &$row, &$params, $page = 0) {
		
	}

	public static function getArticleType($itemId, &$type = null) {
		static $catsOfPhotoalbum = null;
		static $catsOfVideo = null;
		static $catsOfHonorBoard = null;
		if (is_null($catsOfPhotoalbum)){
			$plugin = JPluginHelper::getPlugin('content', 'xprop');
			$params = new JRegistry();
			$params->loadString($plugin->params);

			$catsOfPhotoalbum = $params->get('photoalbum');
			$catsOfVideo = $params->get('video');
			$catsOfHonorBoard = $params->get('honorboard');
		}
		
		if ($catsOfPhotoalbum && in_array($itemId, $catsOfPhotoalbum)) {
			$type = "photoalbum";
			return true;
		} elseif ($catsOfVideo && in_array($itemId, $catsOfVideo)) {
			$type = "video";
			return true;
		} elseif ($catsOfHonorBoard && in_array($itemId, $catsOfHonorBoard)) {
			$type = "honorboard";
			return true;
		}
		return false;
	}

	public static function getExtendedProperty($itemId, $property, &$src = null) {
		static $cache = array();
		if (in_array($itemId . $property, $cache)) {
			$src = $cache[$itemId . $property];
			return TRUE;
		} else {
			jimport('joomla.application.component.modelitem');
			JModelItem::addIncludePath(JPATH_SITE . '/components/com_content/models', 'ContentModel');

			$articleSource = JModelItem::getInstance('Article', 'ContentModel');
			$article = $articleSource->getItem($itemId);
			$attribs = json_decode($article->attribs);
			if (isset($attribs->$property)) {
				$src = $cache[$itemId . $property] = $attribs->$property;
				return TRUE;
			}
			return FALSE;
		}
	}

}
